package com.cjh.mybatis06.bind;


import com.cjh.mybatis06.mapping.MappedStatement;
import com.cjh.mybatis06.mapping.SqlCommandType;
import com.cjh.mybatis06.session.Configuration;
import com.cjh.mybatis06.session.SqlSession;
import lombok.Getter;

import java.lang.reflect.Method;

/**
 * @Descripton 映射器方法
 * @Author cjh
 * @Date 2023/5/26 11:07
 */
public class MapperMethod {
    private final SqlCommand command;

    public MapperMethod(Class<?> mapperInterface, Method method, Configuration configuration){
        this.command = new SqlCommand(configuration,mapperInterface,method);
    }

    public Object execute(SqlSession sqlSession, Object[] args){
        Object res = null;
        switch (command.getType()){
            case INSERT:
                break;
            case DELETE:
                break;
            case UPDATE:
                break;
            case SELECT:
                res  = sqlSession.selectOne(command.getName(),args);
                break;
            default:
                throw new RuntimeException("unknown execution method for:"+command.getName());
        }
        return res;
    }


    /**
     * sql 指令
     */
    @Getter
    public static  class SqlCommand{
        private final String name;
        private final SqlCommandType type;
        public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method){
            String statementName = mapperInterface.getName()+"."+method.getName();
            MappedStatement mappedStatement = configuration.getMappedStatement(statementName);
            name = mappedStatement.getId();
            type = mappedStatement.getSqlCommandType();
        }


    }
}
